<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function getImg() {
      return new Promise((resolve, reject) => {
        const oImg = new Image();
        oImg.onload = function () {
          resolve(oImg)
        }
        oImg.src = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598892402510&di=2b1a6644c867e635a9a18b81337bdc1f&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2F0%2F57be83283fdc9.jpg'
      })
    }
    function timeout() {
      return new Promise((resolve, reject) => {
        setTimeout(() => { reject('超时了') }, 1000)
      })
    }
    Promise.race([
      getImg(), timeout()
    ]).then((res) => {
      console.log(res)
    }).catch(err => {
      console.log(err)
    })
  </script>
</body>

</html>